

import 'package:syria_sports/data/models/error_model.dart';

import '../../core/utils/pref_utils.dart';
import '../../core/utils/show_toast.dart';
import '../../presentation/gyms/gyms_page/models/gym.dart';
import '../../presentation/sports/sports_page/models/sport.dart';
import '../apiClient/api_client.dart';
import '../apiClient/api_keys.dart';
import '../models/data.dart';
import '../remote/dio_helper.dart';

class SportsController{
  static Future<Data?> getAllSports()async{
    var res = await DioHelper.getData( url: ApiClient.getSports, bearerToken: PrefUtils.getToken());

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{
      // showMessage(res.data[ApiKeys.message]);
    }
    Data response = Data.fromJson(res.data);
    return response;
  }
  static Future<Data?> getGymSports({required String gymId})async{
    var res = await DioHelper.getData(
        url: ApiClient.getGymSports,
        query: {
          "gym_id":gymId,
        },
        bearerToken: PrefUtils.getToken(),
    );

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{
      // showMessage(res.data[ApiKeys.message]);
    }
    Data response = Data.fromJson(res.data);
    return response;
  }

  static Future<Data?> getAllPublicSports()async{
    var res = await DioHelper.getData( url: ApiClient.getPublicSports, bearerToken: PrefUtils.getToken());

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{
      // showMessage(res.data[ApiKeys.message]);
    }
    Data response = Data.fromJson(res.data);
    return response;
  }
  static Future<Data?> addSport(Sport sport)async{
    var res = await DioHelper.postData(
      url: ApiClient.sport,
      data: await sport.toJson(),
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }
    return null;
  }

  static Future<Data?> addToGym({required Sport sport,required Gym gym})async{
    var res = await DioHelper.postData(
      url: ApiClient.sportAddToGym,
      data: {
        'sport_id':sport.sportId,
        'gym_id':gym.gymId,
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }
    return null;
  }

  static Future<Data?> getSportById(int id)async{
    var res = await DioHelper.getData(
      url: ApiClient.sport,
      query: {
        'id':id
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else if(!res.data[ApiKeys.state]){
      showMessage(res.data[ApiKeys.message]);
    }else{
      Data response = Data.fromJson(res.data);
      return response;
    }
    return null;
  }

  static Future<Data?> deleteById(String id)async{
    var res = await DioHelper.postData(
      url: ApiClient.sportDelete,
      data: {
        'id':id
      },
      query: {
        'id':id,
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }

    return null;
  }

  static Future<Data?> updateSport(Sport sport)async{
    var res = await DioHelper.postData(
      url: ApiClient.sportUpdate,
      data:await sport.toJson(),
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }

    return null;
  }

}